package com.md.api.service.impl;

import com.md.api.common.utils.DateUtils;
import com.md.api.common.utils.IdUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.common.utils.TokenUtil;
import com.md.api.dao.*;
import com.md.api.entity.*;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.util.Page;
import com.md.api.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author liujie
 * @ClassName ProductServiceImpl
 **/

@Slf4j
@Service
public class CarServiceImpl implements CarService {



    @Resource
    TokenUtil tokenUtil;
    @Resource
    UserCarMapper userCarMapper;
    @Resource
    SysCarTypeMapper carTypeMapper;
    @Resource
    MotOrderMapper motOrderMapper;
    @Resource
    CarFirstAidMapper carFirstAidMapper;
    @Resource
    CarDefaultImgMapper carDefaultImgMapper;

    @Override
    public HttpResult myCar(HttpServletRequest request, ComposeQueryDto qto) {
        Page page = qto.getPage();
        page.initPagingData(UserCar.class);
        String userId = tokenUtil.getUserId(request);
        List<UserCar> list = userCarMapper.myCar(userId,qto.getIsDefault());
        page.setResult(list);
        return new HttpResult(page);
    }



    @Override
    public HttpResult addCar( HttpServletRequest request, UserCar userCar) {
        String carId = IdUtil.uuid();
        userCar.setCarId(carId);
        String userId = tokenUtil.getUserId(request);
        userCar.setUserId(userId);
        if (StringUtils.isNotEmpty(userCar.getModel())) {
//            CarDefaultImg brand = carDefaultImgMapper.getBrand(userCar.getModel());
//            if (null != brand) {
//                userCar.setShowImg(brand.getUrl());
//            }

            CarLogo logo = carDefaultImgMapper.getLogo(userCar.getModel());
            if (null != logo) {
                userCar.setShowImg(logo.getLogo());
            }
        }
        userCarMapper.insert(userCar);
        return HttpResult.OK;
    }

    @Override
    public HttpResult deleteCar(  UserCar userCar) {

        List<MotOrder> orders = motOrderMapper.getByCarId(userCar.getCarId());
        if (!CollectionUtils.isEmpty(orders)){
            return HttpResult.fail("当前车辆存在未完成订单！");
        }
        userCarMapper.delete(userCar.getCarId());
        return HttpResult.OK;
    }

    @Override
    public HttpResult updateCar(  UserCar userCar) {
        UserCar dbCar = userCarMapper.getByCarId(userCar.getCarId());
        if (StringUtils.isNotEmpty(userCar.getModel())&&StringUtils.isNotEmpty(dbCar.getModel())
                &&!dbCar.getModel().equals(userCar.getModel())) {
            CarLogo logo = carDefaultImgMapper.getLogo(userCar.getModel());
            if (null != logo) {
                userCar.setShowImg(logo.getLogo());
            }
        }
        userCarMapper.update(userCar);
        return HttpResult.OK;
    }


    @Override
    public HttpResult setDefault(HttpServletRequest request,UserCar userCar) {
        String userId = tokenUtil.getUserId(request);
        userCar.setUserId(userId);
        userCarMapper.setDefault(userCar);
        return HttpResult.OK;
    }

    @Override
    public HttpResult typeList() {
        List<SysCarType> sysCarTypes = carTypeMapper.selectAll();
        return new HttpResult(sysCarTypes);
    }


    @Override
    public HttpResult firstAid(HttpServletRequest request,CarFirstAid firstAid) {
        String userId = tokenUtil.getUserId(request);
        firstAid.setUserId(userId);
        carFirstAidMapper.insert(firstAid);
        return HttpResult.OK;
    }

    @Override
    public HttpResult logoPage(HttpServletRequest request,ComposeQueryDto qto) {
        Page page = qto.getPage();
        page.initPagingData(CarLogo.class);
        List<CarLogo> list = carDefaultImgMapper.logoList(qto.getKeyword());
        page.setResult(list);
        return new HttpResult(page);
    }

}
